from flask import Flask, request, jsonify
from flask_cors import CORS
from datetime import datetime

app = Flask(__name__)
# 为所有路由添加CORS支持
CORS(app)

# 用于记录请求次数
request_count = 0

@app.route('/api', methods=['POST'])
def api():
    global request_count
    request_count += 1

    # 获取前端传过来的数据
    data = request.get_json()
    question_value = data.get('question_value')

    # 构造返回的数据
    if(data):
        response_data = {
        "id": request_count,
        "question_value": question_value,
        "question_answer": "我们9点关门",
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }
    else:
        response_data={
            "question_answer": "请求无参"
        }

    return jsonify(response_data)

if __name__ == '__main__':
    app.run(debug=True)
    